/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterPageRequest;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterUseRequest;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigCaseApprovalCenterMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigCaseApprovalCenter;
import com.icinfo.cloud.provider.punish.config.service.IConfigCaseApprovalCenterService;
import com.icinfo.cloud.provider.punish.config.vo.ConfigCaseApprovalCenterVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述:  config_case_approval_center 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年04月01日
 */
@Service
public class ConfigCaseApprovalCenterServiceImpl extends MyBatisServiceSupport implements IConfigCaseApprovalCenterService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigCaseApprovalCenterServiceImpl.class);

    @Autowired
    private ConfigCaseApprovalCenterMapper configCaseApprovalCenterMapper;
    /**
     *描述：案审配置分页列表
     * @author liyafeng
     * @date 2022/4/1
      * @param request
     * @return {@link List< ConfigCaseApprovalCenterVo>}
     */
    @Override
    public List<ConfigCaseApprovalCenterVo> getConfigCaseApprovalCenterVoList(ConfigCaseApprovalCenterPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getLength(), request.getOrderBy());
        List<ConfigCaseApprovalCenterVo> configCaseApprovalCenterVos = configCaseApprovalCenterMapper.selectConfigCaseApprovalCenterVoList(request);
        return configCaseApprovalCenterVos;
    }
    /**
     * 保存案审配置
     * @author liyafeng
     * @date 2022/4/1
      * @param configCaseApprovalCenterDto
     * @return {@link int}
     */
    @Override
    public int saveConfigCaseApprovalCenterDto(ConfigCaseApprovalCenterDto configCaseApprovalCenterDto) {
        configCaseApprovalCenterDto.setId(StringUtils.uuid());
        configCaseApprovalCenterDto.setCreateTime(new Date());
        configCaseApprovalCenterDto.setIsUsed("1");
        UserDTO userDTO= UserUtil.getUserInfo();
        configCaseApprovalCenterDto.setCreateUserId(userDTO.getUserId().toString());
        configCaseApprovalCenterDto.setCreateUserName(userDTO.getRealName());
        return configCaseApprovalCenterMapper.insert(configCaseApprovalCenterDto);
    }
    /**
     * 修改案审配置
     * @author liyafeng
     * @date 2022/4/1
      * @param configCaseApprovalCenterDto
     * @return {@link int}
     */
    @Override
    public int editConfigCaseApprovalCenter(ConfigCaseApprovalCenterDto configCaseApprovalCenterDto) {
        ConfigCaseApprovalCenter updateConfigCaseApprovalCenter=new ConfigCaseApprovalCenter();
        BeanUtils.copyProperties(configCaseApprovalCenterDto,updateConfigCaseApprovalCenter);
        if(Constant.STRING_0.equals(configCaseApprovalCenterDto.getApplyAreaType())){
            updateConfigCaseApprovalCenter.setApplyAreaCode("");
            updateConfigCaseApprovalCenter.setApplyAreaName("");
        }
        return configCaseApprovalCenterMapper.updateByPrimaryKeySelective(updateConfigCaseApprovalCenter);
    }
    /**
     * 案审配置启用或禁用
     * @author liyafeng
     * @date 2022/4/1
      * @param param
     * @return {@link int}
     */
    @Override
    public int editUsed(ConfigCaseApprovalCenterUseRequest param) {
        ConfigCaseApprovalCenter updateConfigCaseApprovalCenter=new ConfigCaseApprovalCenter();
        updateConfigCaseApprovalCenter.setIsUsed(param.getIsUsed());
        updateConfigCaseApprovalCenter.setId(param.getId());
        return configCaseApprovalCenterMapper.updateByPrimaryKeySelective(updateConfigCaseApprovalCenter);
    }
    /**
     * 获取案审配置详情
     * @author liyafeng
     * @date 2022/4/1
      * @param id
     * @return {@link ConfigCaseApprovalCenter}
     */
    @Override
    public ConfigCaseApprovalCenter getDetail(String id) {
        return configCaseApprovalCenterMapper.selectByPrimaryKey(id);
    }
    /**
     * 校验案审配置是否存在
     * @author liyafeng
     * @date 2022/4/8
      * @param configCaseApprovalCenterDto
     * @return {@link boolean}
     */
    @Override
    public boolean checkIsExist(ConfigCaseApprovalCenterDto configCaseApprovalCenterDto) {
        boolean isExist=false;
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("applyAreaType",configCaseApprovalCenterDto.getApplyAreaType());
        paramMap.put("applyAreaCode",configCaseApprovalCenterDto.getApplyAreaCode());
        List<ConfigCaseApprovalCenterVo> configCaseApprovalCenters=configCaseApprovalCenterMapper.selectConfigCaseApprovalCentersByMap(paramMap);
        if(null!=configCaseApprovalCenters&&configCaseApprovalCenters.size()>0){
            isExist=true;
        }
        return isExist;
    }
}